/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.trick.admin.cell;

import it.trick.admin.App;
import it.trick.admin.controller.ChannelTopController;
import it.trick.admin.controller.DeleteContentsViewController;
import it.trick.admin.controller.SelectChannelController;
import it.trick.admin.model.ChannelModel;
import it.trick.admin.model.ContentsBaseModel;
import java.util.ArrayList;
import java.util.List;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.ListCell;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;

/**
 *
 * @author takahara
 */
public class ChannelListViewCell extends ListCell<ChannelModel> {

    private ListCell listCell;
    private ContextMenu contextMenu = new ContextMenu();

    /**
     * コンストラクタ
     */
    public ChannelListViewCell() {
        setListCell();

        MenuItem changeIndexUpperMenuItem = new MenuItem("上へ ↑");
        MenuItem changeIndexLowerMenuItem = new MenuItem("下へ ↓");
        MenuItem deletePageMenuItem = new MenuItem("削除");

        contextMenu.getItems().addAll(
                changeIndexUpperMenuItem,
                changeIndexLowerMenuItem,
                new SeparatorMenuItem(),
                deletePageMenuItem);

        // ---------------------------------------------------------------------
        // 削除イベント
        // ---------------------------------------------------------------------
        deletePageMenuItem.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                App.getInstance().replacePopSceneContent(getDeleteStageController());
            }
        });

        // ---------------------------------------------------------------------
        // 上移動イベント
        // ---------------------------------------------------------------------
        changeIndexUpperMenuItem.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {

                ChannelModel channelModel = getItem();

                int nIndex = getListView().getItems().indexOf(getItem());
                if(nIndex > 0) {
                    getListView().getItems().remove(channelModel);
                    getListView().getItems().add(nIndex - 1, channelModel);

                    getListView().getSelectionModel().select(channelModel);

                    changeIndex();
                }
            }
        });

        // ---------------------------------------------------------------------
        // 下移動イベント
        // ---------------------------------------------------------------------
        changeIndexLowerMenuItem.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                ChannelModel channelModel = getItem();

                int nIndex = getListView().getItems().indexOf(getItem());
                if(nIndex < getListView().getItems().size() - 1) {
                    getListView().getItems().remove(channelModel);
                    getListView().getItems().add(nIndex + 1, channelModel);

                    getListView().getSelectionModel().select(channelModel);

                    changeIndex();
                }
            }
        });
    }

    private void setListCell() {
        listCell = this;
    }

    @Override
    public void updateItem(ChannelModel item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            super.setText(item.getName());

            // コンテキストメニュー
            setContextMenu(contextMenu);
            this.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent t) {
                    // コンテキストメニュー表示イベント
                    if (t.isControlDown()) {
                        getContextMenu().show(listCell, t.getScreenX(), t.getScreenY());

                    } else if (t.getClickCount() == 2) {
                        App.getInstance().closePopStage();

                        ChannelTopController.getInstance().setup(getItem());
                    }
                }
            });
        }
    }

    /**
     * コンテンツ削除
     * @return
     */
    private Pane getDeleteStageController() {
        ContentsBaseModel baseModel = getItem();
        DeleteContentsViewController controller = new DeleteContentsViewController(baseModel.getName());

        controller.deleteButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {

                // POST処理
                SelectChannelController.getInstance().deleteChannelTask.setup(getItem().getKeyValueString());
                SelectChannelController.getInstance().deleteChannelTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
                    @Override
                    public void handle(WorkerStateEvent t) {
                        getListView().getItems().remove(getItem());
                    }
                });
                SelectChannelController.getInstance().deleteChannelTask.restart();

                App.getInstance().closePopStage();
            }
        });

        return controller;
    }

    /**
     * 順序の更新
     */
    private void changeIndex() {

        List<ContentsBaseModel> modelList = new ArrayList();

        for(ChannelModel model: getListView().getItems()) {
            modelList.add((ContentsBaseModel)model);
        }

        SelectChannelController.getInstance().changeIndexTask.setup(modelList);
        SelectChannelController.getInstance().changeIndexTask.restart();

    }
}
